﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class MyOrderDetail : BasePage
    {
        public static MLMDataContext dtct = new MLMDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            MLMDataContext ct = new MLMDataContext();
            long invoiceID = long.Parse(Request.QueryString["ID"].ToString());
            long userID = Permission.GetCurrentUserId();
            var status = ct.Invoices.FirstOrDefault(it => it.InvoiceID == invoiceID).Status;
            if (ct.Invoices.FirstOrDefault(it => it.InvoiceID == invoiceID && it.UserID == userID) == null)
            {
                Response.Write("Access denied!");
                Response.End();
                RptInvoiceDetail.Visible = false;
            }
            PanelChangeStatus.Visible = status == -1;
            SaveChangeStatus.Click += new EventHandler(SaveChangeStatus_Click);
        }
        void SaveChangeStatus_Click(object sender, EventArgs e)
        {
            MLMDataContext ct = new MLMDataContext();
            long InvoiceID = long.Parse(Request.QueryString["ID"]);
            var invoice = ct.Invoices.FirstOrDefault(it => it.InvoiceID == InvoiceID);
            invoice.Status = int.Parse(StatusSelect.SelectedValue);
            ct.SaveChanges();
            Response.Redirect(Request.RawUrl);
        }
        public static string FormatPrice(object price, object tax)
        {
            try
            {
                decimal _price = decimal.Parse(price.ToString());
                decimal _tax = decimal.Parse(tax.ToString());
                return (Math.Round(_price + _price * (_tax / 100), 2)).ToString() + " &euro;";
            }
            catch (Exception ex) { return "0"; }
        }
        public static long GetCountryIDByDepoId(object depoid)
        {
            try
            {
                long _depoid = long.Parse(depoid.ToString());
                MLMDataContext ct = new MLMDataContext();
                return long.Parse(ct.Users.FirstOrDefault(it => it.UserId == _depoid).CountryID.ToString());
            }
            catch (Exception ex)
            {
                return 0;
            }

        }
    }
}